document.writeln('<script src="js/crypto-js.js"><' + '/' + 'script>');
document.writeln('<script src="js/verify.js"><' + '/' + 'script>');
$(document).ready(function(){
    //初始化图形验证码组件
    var typeArrays = ["clickWord", "blockPuzzle"];
    var randomNumber = Math.floor(Math.random() * (typeArrays.length));
    if ("clickWord" == typeArrays[randomNumber]) {
        pointsVerifyInit();
    } else if ("blockPuzzle" == typeArrays[randomNumber]) {
        slideVerifyInit();
    }
    $('#getVerifyCode').on("click", function(){
        if($("#mobile").val() == null || $("#mobile").val() == ""){
            $('.tips').removeClass('none').html('请输入手机号');
            $(".tips").css("color","red");
            return false;
        }
        sendCode();
    });
});
var baseUrl = "http://127.0.0.1:11111";

function slideVerifyInit(){
    $('#mpanel1').slideVerify({
        baseUrl: baseUrl,
        mode:'pop',     //展示模式
        containerId:'pwdLogin,codeLogin',//pop模式 必填 被点击之后出现行为验证码的元素id
        imgSize : {       //图片的大小对象,有默认值{ width: '310px',height: '155px'},可省略
            width: '400px',
            height: '200px',
        },
        barSize:{          //下方滑块的大小对象,有默认值{ width: '310px',height: '50px'},可省略
            width: '400px',
            height: '40px',
        },
        beforeCheck:function(){  //检验参数合法性的函数  mode ="pop"有效
            var flag = true;
            if($("#mobile").val() == null || $("#mobile").val() == ""){
                $('.tips').removeClass('none').html('请输入手机号');
                $(".tips").css("color","red");
                return false;
            }
            if ($('input[name=loginType]').val() == 1 && ($("#password").val() == null || $("#password").val() == "")) {
                $('.tips').removeClass('none').html('请输入密码');
                $(".tips").css("color","red");
                return false;
            }else if($('input[name=loginType]').val() == 2 && ($("#code").val() == null || $("#code").val() == "")) {
                $('.tips').removeClass('none').html('请输入验证码');
                $(".tips").css("color","red");
                return false;
            }
            return flag
        },
        ready : function() {},  //加载完毕的回调
        success : function(params) { //成功的回调
            if ($('input[name=loginType]').val() == 1) {
                loginPwd();
            }else if($('input[name=loginType]').val() == 2 ) {
                loginCode();
            }
        },
        error : function() {}        //失败的回调
    });
}

function pointsVerifyInit() {
    $('#mpanel2').pointsVerify({
        baseUrl: baseUrl,
        containerId: 'pwdLogin,codeLogin',
        mode: 'pop',
        imgSize: {
            width: '400px',
            height: '200px',
        },
        beforeCheck: function () {
            var flag = true;
            if($("#mobile").val() == null || $("#mobile").val() == ""){
                $('.tips').removeClass('none').html('请输入手机号');
                $(".tips").css("color","red");
                return false;
            }
            if ($('input[name=loginType]').val() == 1 && ($("#password").val() == null || $("#password").val() == "")) {
                $('.tips').removeClass('none').html('请输入密码');
                $(".tips").css("color","red");
                return false;
            }else if($('input[name=loginType]').val() == 2 && ($("#code").val() == null || $("#code").val() == "")) {
                $('.tips').removeClass('none').html('请输入验证码');
                $(".tips").css("color","red");
                return false;
            }
            return flag
        },
        ready: function () {
        },
        success: function (params) {
            if ($('input[name=loginType]').val() == 1) {
                loginPwd();
            }else if($('input[name=loginType]').val() == 2 ) {
                loginCode();
            }
        },
        error: function () {
        }
    });
}

function loginPwd(){
    $.ajax({
        type : "post",
        contentType: "application/json;charset=UTF-8",
        url : baseUrl + "/login/password",
        data :JSON.stringify({mobile:$("#mobile").val(),password:$("#password").val()}),
        success:function(res){
            if(res.repCode == "101"){
                $('.tips').removeClass('none').html(res.repMsg);
                $(".tips").css("color","blue");
            }else {
                $('.tips').removeClass('none').html(res.repMsg);
                $(".tips").css("color","red");
            }
        },
        fail: function(err) {

        }
    })
}

function loginCode(){
    $.ajax({
        type : "post",
        contentType: "application/json;charset=UTF-8",
        url : baseUrl + "/login/code",
        data :JSON.stringify({mobile:$("#mobile").val(),code:$("#code").val()}),
        success:function(res){
            if(res.repCode == "101"){
                $('.tips').removeClass('none').html(res.repMsg);
                $(".tips").css("color","blue");
            }else {
                $('.tips').removeClass('none').html(res.repMsg);
                $(".tips").css("color","red");
            }
        },
        fail: function(err) {

        }
    })
}

function sendCode(){
    $.ajax({
        type : "post",
        contentType: "application/json;charset=UTF-8",
        url:  baseUrl + "/sendCode",
        data: JSON.stringify({mobile:$("#mobile").val()}),
        success: function (res) {
            if (res.repCode == "101"){
                var time = res.repData;
                var timer = setInterval(function () {
                    $(".getVerifyCode").html(time + '秒后重试')
                    $(".getVerifyCode").css({
                        "pointerEvents": "none"
                    })
                    if (time == 0) {
                        clearInterval(timer)
                        $(".getVerifyCode").html('获取验证码')
                        $(".getVerifyCode").css({
                            "pointerEvents": "all"
                        })
                    }
                    time = time - 1
                }, 1000);
            }else {
                $('.tips').removeClass('none').html(res.repMsg);
            }
        },
        fail: function (data) {
            console.log(data);
        }
    });
}

function switchLogin(loginType) {
    if (loginType != 1 && loginType != 2) {
        return;
    }
    $('.tips').addClass('none')
    if (loginType == 2) {
        $('input[name=loginType]').val(2);
        $(".verifyPwd").hide();
        $(".verifyCode").show();
        $("#code").focus();
    } else {
        $('input[name=loginType]').val(1);
        $(".verifyPwd").show();
        $(".verifyCode").hide();
        $("#password").focus();
    }
}
